//
//  VCPassenger.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/23.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol VCPassengerDelegate {
    
    func returnContacts(beans:[UserInfo])
}

class VCPassenger: VCBase {
    
    // 是否显示确定按钮
    public var isShowOK:Bool = false
    // 是否显示复选按钮
    public var isShowCheckBox: Bool = false
    // 代理
    public var delegate: VCPassengerDelegate?
    
    public var tempPassenger:[UserInfo]?
    
    private lazy var passengerView:PassengerView = {
        let ui = PassengerView()
        let uiY = backroundWhite.frame.maxY + MARGIN10
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_set()
        init_view()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        init_data()
        init_event()
    }
}

// MARK: 逻辑部分
extension VCPassenger {
    
    private func init_event() -> Void {
        // 复选框
        if isShowCheckBox {
            passengerView.isShowCheckBox = true
        }
        
        // 圆加号点击
        cirleAddView.complete = {
            let vc = VCPassengerManger()
            self.pushVc(title: "添加乘机人", vc: vc)
        }
        
        // 编辑点击
        passengerView.complete = {item in
            let vc = VCPassengerManger()
            vc.isShowDelete = true
            vc.isEditAllInfo = true
            vc.userInfo = self.tempPassenger?[item]
            self.pushVc(title: "编辑乘机人", vc: vc)
        }        
    }
    
    private func init_data() -> Void {
        guard let userid = UserDefaults.standard.value(forKey: SP_USERID) else {
            return
        }
        // 拼接数据
        let dict = NSMutableDictionary()
        dict.setValue(userid, forKey: SP_USERID)
        // 请求
        self.doPost(api: "search/passenger", dict: dict) { (dict) in
            if self.isServerSuccess(dict: dict) != 1200 {
                return
            }
            var temp = [UserInfo]()
            let arrs = dict.value(forKey: SP_BODYREQUEST) as! NSArray
            for item in arrs {
                let info = UserInfo(dict: item as! [String : Any])
                temp.append(info)
            }
            self.tempPassenger = temp
            self.passengerView.userInfos = temp
        }
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(passengerView)
        view.addSubview(cirleAddView)
    }
    
    @objc func returnData() -> Void {
        guard let passengers = tempPassenger else {
            return
        }
        let arrs = passengerView.checkBoxSelectTag.allValues
        if arrs.count == 0 {
            return
        }
        var temp = [UserInfo]()
        for i in arrs {
            let bean = passengers[i as! Int]
            temp.append(bean)
        }
        delegate?.returnContacts(beans: temp)
        self.navigationController?.popViewController(animated: true)
    }
    
    private func init_set() -> Void {
        if isShowOK {
            let okItem = UIBarButtonItem(title: "确定", style: .done, target: self, action: #selector(returnData))
            navigationItem.rightBarButtonItem = okItem
        }
    }
}
